package com.wu.web;

import com.wu.bean.Comment;
import com.wu.model.JournalModel;
import com.wu.service.CommentService;
import com.wu.service.JournalService;
import com.wu.service.impl.CommentServiceImpl;
import com.wu.service.impl.JournalServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 添加文章评论
 */
@WebServlet("/AddComment")
public class AddCommentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int reviewarticles=Integer.parseInt(req.getParameter("reviewarticles"));
        String comments=req.getParameter("comments");
        HttpSession session=req.getSession();
        int reviewers=Integer.parseInt(session.getAttribute("userid").toString());
        if("".equals(reviewers))
        {
            resp.getWriter().print("no");
            return;
        }
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取网络时间
        Date time=null;
        try {
            time= dateFormat.parse(getWebsiteDatetime("http://www.ntsc.ac.cn"));
        }catch (ParseException e){}
        CommentService commentService=new CommentServiceImpl();
        JournalService journalService=new JournalServiceImpl();
        int ok = commentService.AddComment(new Comment(null, reviewarticles, reviewers, comments, time));
        //修改文章评论量
        JournalModel journalModel = journalService.QueryLog(reviewarticles);
        int updateCommentolume = journalService.updateCommentolume(reviewarticles, journalModel.getCommentolume() + 1);
        if(ok>0)
            resp.getWriter().print("ok");
        else
            resp.getWriter().print("no");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
    /**
     * 获取指定网站的日期时间
     *
     * @param webUrl
     * @return
     */
    private static String getWebsiteDatetime(String webUrl){
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间
            Date date = new Date(ld);// 转换为标准时间对象
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);// 输出北京时间
            return sdf.format(date);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
